package com.imooc.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.imooc.web.dto.User;
import com.imooc.web.exception.UserNotExistException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang.StringUtils;
import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private ProviderSignInUtils providerSignInUtils;

    @GetMapping("/me")
    public Object  me(Authentication authentication,HttpServletRequest request) throws UnsupportedEncodingException {
        String authenticationHeader = request.getHeader("Authorization");
        String token = StringUtils.substringAfter(authenticationHeader,"bearer ");
        JwtParser jwtParser = Jwts.parser().setSigningKey("123".getBytes(StandardCharsets.UTF_8));
        Claims body = jwtParser.parseClaimsJws(token).getBody();
        String company = (String) body.get("company");
        System.out.println(company);

        return SecurityContextHolder.getContext().getAuthentication();
    }

    @GetMapping
    @JsonView(User.UserSimpleView.class)
    public List<User> get() throws InterruptedException {
        Thread.sleep(3000);

        ArrayList<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }

    @JsonView(User.UserDetailView.class)
    @GetMapping("/{id}")
    public User getInfo(@PathVariable("id") Integer id){

        User user = new User();
        user.setUsername("tom");
        return user;
    }

    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult errors){
        System.out.println(errors.hasErrors());
        System.out.println(errors);

        System.out.println(user.getPassword());
        System.out.println(user.getUsername());
        System.out.println(user.getBirthday());
        return user;
    }

    @PutMapping("/{id}")
    public User update( @RequestBody User user,@PathVariable("id")Integer id) throws InterruptedException {
        // if (errors.hasErrors()){
        //     errors.getAllErrors().forEach(error -> System.out.println(error.getDefaultMessage()));
        // }

        // System.out.println(user.getPassword());
        // System.out.println(user.getUsername());
        // System.out.println(user.getBirthday());
        // return user;
        throw new UserNotExistException("1");
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id")Integer id){
        System.out.println(id);
    }

    @PostMapping("/user/register")
    public void register(User user, HttpServletRequest request){
        // 无论是注册用户还是绑定用户，要先拿到用户的唯一标示。
        String userId = user.getUsername();
        ServletWebRequest servletWebRequest = new ServletWebRequest(request);
        providerSignInUtils.doPostSignUp(userId,servletWebRequest);

    }
}
